package org.takeback.chat.utils;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import sun.misc.BASE64Encoder;

public class ImageUtils {
	// log
	private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class);

	public static String imgUtils(String url) {

		File file = QRCode.from(url).withSize(300, 300).to(ImageType.PNG).file();
		// System.out.println(ImageUtils.imgUtils("share_bg.jpg", file, "1",
		// "10"));
		try {

			URL bgImgUrl = ImageUtils.class.getClassLoader().getResource("image/share_bg.jpg");
			File bgImgFfile = new File(bgImgUrl.getFile());

			BufferedImage qrBuffer = ImageIO.read(file).getSubimage(20, 20, 260, 260);// 二维码图

			BufferedImage bgImgBuffer = ImageIO.read(bgImgFfile);// 背景图
			int x = (bgImgBuffer.getWidth() - qrBuffer.getWidth()) / 2 - 25; // 获取二维码要放在的位置
			int y = (bgImgBuffer.getHeight() - qrBuffer.getHeight()) / 2 + 330;
			Graphics2D g = bgImgBuffer.createGraphics();
			g.drawImage(qrBuffer, x + 40, y, qrBuffer.getWidth(), qrBuffer.getHeight(), null);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ImageIO.write(bgImgBuffer, "png", os);
			InputStream is = new ByteArrayInputStream(os.toByteArray());
			ImageIO.write(bgImgBuffer, "png", new File("D:/n11ew.png"));
			return GetImageStr(is);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return null;
	}

	public static void main(String[] args) {
		// File f = new File("d:/new.png");
		// f.delete();
		// imgUtils("1.png", "2.png", "意难忘", "10");
		ImageUtils.imgUtils("http://www.tesuf.com/#/register?inviteId=33");
	}

	// 图片转化成base64字符串
	public static String GetImageStr(InputStream in) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		byte[] data = null;
		// 读取图片字节数组
		try {
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);// 返回Base64编码过的字节数组字符串
	}

//	/**
//	 * @Title: compressPic @Description: 压缩图片,通过压缩图片质量，保持原图大小 @param
//	 * quality:0-1 @return byte[] @throws
//	 */
//	public static byte[] compressPic(byte[] imageByte, float quality) {
//		byte[] inByte = null;
//		try {
//			ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte);
//			Image img = ImageIO.read(byteInput);
//			float newWidth = img.getWidth(null);
//			float newHeight = img.getHeight(null);
//
//			Image image = img.getScaledInstance((int) newWidth, (int) newHeight, Image.SCALE_SMOOTH);
//
//			// 缩放图像
//			BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
//			Graphics2D g = tag.createGraphics();
//			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
//			g.dispose();
//			ByteArrayOutputStream out = new ByteArrayOutputStream(imageByte.length);
//			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//			JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
//			/* 压缩质量 */
//			jep.setQuality((float) 0.6, true);
//			encoder.encode(tag, jep);
//			inByte = out.toByteArray();
//			out.close();
//
//		} catch (IOException ex) {
//			ex.printStackTrace();
//		}
//		return inByte;
//	}
}
